import 'package:flutter_give/sample/configure/constants.dart';
import 'package:flutter_give/utils/persistent_storage.dart';
import 'package:rapid_kit/rapid_kit.dart';

/// 探鸽初始化类
class RapidInitialize {
  RapidInitialize._();

  static Future<bool> init() async {
    String appId = "";
    String packageName = "";
    int apiEnv = 0;

    String? localAppId = await SharedPreferencesUtil.instance.getString(SampleConstants.storageKeyAppConfigAppId);
    String? localPackageName = await SharedPreferencesUtil.instance.getString(SampleConstants.storageKeyAppConfigPackageName);
    int? localApiEnv = await SharedPreferencesUtil.instance.getInt(SampleConstants.storageKeyAppConfigApiEnv);

    if (localAppId?.isNotEmpty == true) {
      appId = localAppId!;
    } else {
      appId = SampleConstants.defaultRapidAppID;
    }

    if (localPackageName?.isNotEmpty == true) {
      packageName = localPackageName!;
    } else {
      packageName = SampleConstants.defaultRapidPackageName;
    }

    if (localApiEnv != null) {
      apiEnv = localApiEnv;
    }

    bool runtimeLogging = (await SharedPreferencesUtil.instance.getInt(SampleConstants.storageKeyAppConfigRuntimeLogging) ?? 0) == 1;

    final initialized = await RapidKit.initialize(Configurations(
        id: appId, package: packageName, httpLogging: runtimeLogging, debugging: runtimeLogging, environment: Environment.values[apiEnv]));

    return initialized;
  }
}
